# coding:utf-8
import json
from flask import Flask, request
import detect as phone
import climb.detect as climb
import follow.detect as follow

app = Flask(__name__)
MY_URL = '/knowledge/api/v1/'

# get
# @app.route(MY_URL + 'get/tasks', methods=['GET'])
# def get_task():
#     param = request.args.to_dict()
#     return str(text_process.handle_client(param))


# post
@app.route(MY_URL + 'post/ocr', methods=['POST'])
def post_task():
    param = request.json
    # print("input" + str(param))
    type = param['type']
    result_list = []
    response = {}
    if type is not None:
        if "," in type:
        # 循环调用后端模型
            type_arr = type.split(",")
            size = len(type_arr)
            for i in range(0, size):
                validate_type = type_arr[i]
                print("validate_type:  " + validate_type)
                param['type'] = validate_type
                if validate_type == "phone":
                    data_json = phone.main(param)
                    result_list.append(json.loads(data_json))
                if validate_type == "climb":
                    data_json = climb.main(param)
                    result_list.append(json.loads(data_json))
                if validate_type == "follow":
                    data_json = follow.main(param)
                    result_list.append(json.loads(data_json))
            response['returnCode'] = "0000"
            response['returnData'] = result_list
            return response
        else:
        #调用一次后端模型
            if type == "phone":
                print("调用一次phone模型")
                rst_json = phone.main(param)
            if type == "climb":
                print("调用一次climb模型")
                rst_json = climb.main(param)
            if type == "follow":
                print("调用一次follow模型")
                rst_json = follow.main(param)
            response['returnCode'] = "0000"
            response['returnData'] = json.loads(rst_json)
            print(response)
            return response
    else:
        return {"returnCode":"0001","returnData":[]}


if __name__ == '__main__':
    app.run(debug=False, port=5000, host="0.0.0.0")
    # app.run(debug=False)
# http://127.0.0.1:5000/knowledge/api/v1/get/tasks?keystr="服务器上架"&topN=3